// NDSFile.cpp: implementation of the CNDSFile class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "NDSFile.h"
#include "../Graphic/Device.h"
#ifdef IPHONEOS
#include "FileSystemIPhone.h"
#include "Application.h"
#import  <stdio.h>
#endif

#ifdef IPHONEOS
extern const char g_AppPath[1024];
extern const char g_AppDocumentPath[1024];
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CNDSFile::CNDSFile()
{
	m_pFile = NULL;
	m_pWFile = NULL;
}

CNDSFile::~CNDSFile()
{
	Close();
}

#if 0

void CNDSFile::Close()
{
	if(m_pFile)
	{
		//m_pFile->drop();
		Application::GetInstance()->GetFileSystem()->closeFile(m_pFile);
		m_pFile = NULL;
	}
}

BOOL CNDSFile::Open(const char *szFilePath)
{
	Application *app = Application::GetInstance();
	m_pFile = Application::GetInstance()->GetFileSystem()->openResource(szFilePath);
	//m_pFile = Application::GetInstance()->GetFileSystem()->openSavefile(szFilePath, false);
	//irr::io::IFileSystem* fileSystem = CM3DDevice::GetM3DDevice()->m_irrFileSystem;
	//m_pFile = fileSystem->createAndOpenFile(szFilePath);
	if(m_pFile)
		return TRUE;
	else
		return FALSE;
}

BOOL CNDSFile::OpenSaveFile(const char *szFilePath,bool forWriting)
{
	Application *app = Application::GetInstance();
	if(forWriting)
	{
		m_pWFile = Application::GetInstance()->GetFileSystem()->openSavefile(szFilePath, forWriting);
	}
	else
	{
		m_pFile = Application::GetInstance()->GetFileSystem()->openSavefile(szFilePath, forWriting);
	}
	if(m_pWFile || m_pFile)
		return TRUE;
	else
		return FALSE;
}

long CNDSFile::Read(void *out_pData, long in_nSize)
{
	
	
	m_pFile->read((byte*)out_pData, in_nSize);
	return in_nSize;
}

long CNDSFile::Write(void * in_pData, long in_nSize)
{
	long bytesWrite = m_pWFile->write((byte*)in_pData,in_nSize);
	return bytesWrite;
}

float CNDSFile::ReadFloat()
{
	float nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}
int CNDSFile::ReadLong()
{
	int nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}

int CNDSFile::ReadInt()
{
	int nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}

short CNDSFile::ReadShort()
{
	short nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}
signed char CNDSFile::ReadChar()
{
	signed char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned long CNDSFile::GetFileSize()
{
	unsigned long nSize = m_pFile->size();
	return nSize;
}

void CNDSFile::Advance(int nByteCount)
{
	m_pFile->seek(nByteCount);
}

BOOL CNDSFile::Seek(int mode,int offset)
{
	if(mode == SEEKMODE_SET)
		 m_pFile->seek(offset/*,false*/);
	else if(mode == SEEKMODE_END)
		 m_pFile->seek(m_pFile->size() - offset/*,false*/);
	else
		 m_pFile->seek(offset/*,true*/);
	return true;
}

unsigned short CNDSFile::ConvertReverse(unsigned short nSrc)
{
	unsigned short low = (nSrc & 0xFF)<<8;
	unsigned short high = (nSrc & 0xFF00)>>8;
	return low | high;
}

unsigned short CNDSFile::ReadUShortReverse()
{
	unsigned short nCount;
	Read(&nCount,sizeof(unsigned short));
	nCount = ConvertReverse(nCount);
	return nCount;
}

short CNDSFile::ReadShortReverse()
{
	signed short nCount;
	Read(&nCount,sizeof(signed short));
	nCount = ConvertReverse(nCount);
	return nCount;
}

unsigned short CNDSFile::ReadUnsignedShort()
{
	unsigned short nCount;
	Read(&nCount,sizeof(unsigned short));
	return nCount;
}

signed char CNDSFile::ReadCharReverse()
{
	signed char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned char CNDSFile::ReadUCharReverse()
{
	unsigned char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned char CNDSFile::ReadUnsignedByte()
{
	unsigned char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned int CNDSFile::ReadIntReverse()
{
	unsigned int nCount;
	Read(&nCount,4);
	nCount = ConvertIntReverse(nCount);
	return nCount;
}

unsigned int CNDSFile::ConvertIntReverse(unsigned int nSrc)
{
	unsigned int n1 = (nSrc & 0xFF000000)>>24;
	unsigned int n2 = (nSrc & 0xFF0000)>>8;
	unsigned int n3 = (nSrc & 0xFF00)<<8;
	unsigned int n4 = (nSrc & 0xFF)<<24;
	return n1 | n2 | n3 | n4;
}

#else
void CNDSFile::Close()
{
	if(m_pFile)
	{
		m_pFile->drop();
		m_pFile = NULL;
	}
	if(m_pWFile)
	{
		m_pWFile->drop();
		m_pWFile = NULL;
	}
}

BOOL CNDSFile::Open(const char *szFilePath)
{
	irr::io::IFileSystem* fileSystem = CDevice::Instance()->GetFileSystem();	
	m_pFile = fileSystem->createAndOpenFile(szFilePath);
	if(m_pFile)
		return TRUE;
	else
		return FALSE;
}
//#ifndef IPHONEOS
BOOL CNDSFile::OpenSaveFile(const char *szFilePath, bool forWriting)
{
#ifdef IPHONEOS
	chdir(g_AppDocumentPath);	
#endif
	if(!forWriting)
	{
		irr::io::IFileSystem* fileSystem = CDevice::Instance()->GetFileSystem();
		m_pFile = fileSystem->createAndOpenFile(szFilePath);
	}
	else
	{
		irr::io::IFileSystem* fileSystem = CDevice::Instance()->GetFileSystem();
		m_pWFile = fileSystem->createAndWriteFile(szFilePath);
	}
#ifdef IPHONEOS
	chdir(g_AppPath);
#endif
	if(forWriting)
	{
		if(m_pWFile)
			return TRUE;
		else
			return FALSE;
	}
	else
	{
		if(m_pFile)
			return TRUE;
		else
			return FALSE;
	}

}
//#endif

long CNDSFile::Read(void *out_pData, long in_nSize)
{
	long bytesRead = m_pFile->read(out_pData, in_nSize);
	return bytesRead;
}

//#ifndef IPHONEOS
long CNDSFile::Write(void * in_pData, long in_nSize)
{
#ifdef IPHONEOS
	chdir(g_AppDocumentPath);
#endif
	long bytesWrite = m_pWFile->write(in_pData,in_nSize);
#ifdef IPHONEOS
	chdir(g_AppPath);
#endif
	return bytesWrite;
}
//#endif

float CNDSFile::ReadFloat()
{
	float nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}
int CNDSFile::ReadLong()
{
	int nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}

int CNDSFile::ReadInt()
{
	int nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}

short CNDSFile::ReadShort()
{
	short nRet;
	Read(&nRet,sizeof(nRet));
	return nRet;
}
signed char CNDSFile::ReadChar()
{
	signed char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned long CNDSFile::GetFileSize()
{
	unsigned long nSize = m_pFile->getSize();
	return nSize;
}

void CNDSFile::Advance(int nByteCount)
{
	m_pFile->seek(nByteCount,true);
}

BOOL CNDSFile::Seek(int mode,int offset)
{
	if(mode == SEEKMODE_SET)
		return m_pFile->seek(offset,false);
	else if(mode == SEEKMODE_END)
		return m_pFile->seek(GetFileSize() - offset,false);
	else
		return m_pFile->seek(offset,true);
}

unsigned short CNDSFile::ConvertReverse(unsigned short nSrc)
{
	unsigned short low = (nSrc & 0xFF)<<8;
	unsigned short high = (nSrc & 0xFF00)>>8;
	return low | high;
}

unsigned short CNDSFile::ReadUShortReverse()
{
	unsigned short nCount;
	Read(&nCount,sizeof(unsigned short));
	nCount = ConvertReverse(nCount);
	return nCount;
}

unsigned short CNDSFile::ReadUnsignedShort()
{
	unsigned short nCount;
	Read(&nCount,sizeof(unsigned short));
	return nCount;
}

short CNDSFile::ReadShortReverse()
{
	signed short nCount;
	Read(&nCount,sizeof(signed short));
	nCount = ConvertReverse(nCount);
	return nCount;
}

signed char CNDSFile::ReadCharReverse()
{
	signed char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned char CNDSFile::ReadUCharReverse()
{
	unsigned char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned char CNDSFile::ReadUnsignedByte()
{
	unsigned char nCount;
	Read(&nCount,1);
	return nCount;
}

unsigned int CNDSFile::ReadIntReverse()
{
	unsigned int nCount;
	Read(&nCount,4);
	nCount = ConvertIntReverse(nCount);
	return nCount;
}

unsigned int CNDSFile::ConvertIntReverse(unsigned int nSrc)
{
	unsigned int n1 = (nSrc & 0xFF000000)>>24;
	unsigned int n2 = (nSrc & 0xFF0000)>>8;
	unsigned int n3 = (nSrc & 0xFF00)<<8;
	unsigned int n4 = (nSrc & 0xFF)<<24;
	return n1 | n2 | n3 | n4;
}
#endif
